#define _CRT_SECURE_NO_WARNINGS  1
#include<stdio.h>
#include<stdlib.h>
typedef struct HuffmanTreeNode
{
    int data;
    int parent, left, right;
}HTNode, * HT;
int w[100002];
int sum;
void Test(int n);
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &w[i]);
    Test(n);
    printf("%d", sum);
    return 0;
}
void Test(int n)
{
    if (n <= 1)
        return;
    int m = 2 * n - 1;
    HT h = (HT)malloc(sizeof(HTNode) * (m + 1));
    int i = 0, j = 0;
    for (i = 0; i < n; i++)
    {
        h[i].data = w[i];
        h[i].parent = -1;
        h[i].left = -1;
        h[i].right = -1;
    }
    for (i; i < m; i++)
    {
        h[i].data = 0;
        h[i].parent = -1;
        h[i].left = -1;
        h[i].right = -1;
    }
    for (i = n; i < m; i++)
    {
        int x1 = 0, x2 = 0, m1 = 0x2f2f2f2f, m2 = 0x2f2f2f2f;
        for (j = 0; j < i; j++)
        {
            if (h[j].parent == -1 && h[j].data < m1)
            {
                m2 = m1;
                x2 = x1;
                m1 = h[j].data;
                x1 = j;
            }
            else if (h[j].parent == -1 && h[j].data < m2)
            {
                m2 = h[j].data;
                x2 = j;
            }
        }
        h[i].data = m1 + m2;
        h[i].left = x1;
        h[i].right = x2;
        h[x1].parent = i;
        h[x2].parent = i;
        sum = h[i].data + sum;
    }
}